<template>
  <div class="ssh-terminal">
    <el-card class="terminal-card">
      <template #header>
        <div class="card-header">
          <div class="title">
            <el-icon><Terminal /></el-icon>
            <span>SSH终端</span>
          </div>
          <el-button type="primary" @click="showConnectDialog">
            <el-icon><Plus /></el-icon>
            新建连接
          </el-button>
        </div>
      </template>

      <Terminal
        v-if="showTerminal"
        :host="connection.host"
        :user="connection.user"
        :password="connection.password"
      />
      <el-empty v-else description="点击新建连接开始使用终端" />
    </el-card>

    <!-- 连接配置对话框 -->
    <el-dialog
      v-model="connectDialogVisible"
      title="新建SSH连接"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" status-icon>
        <el-form-item label="主机地址" prop="host">
          <el-input v-model="form.host" placeholder="请输入主机地址" />
        </el-form-item>
        <el-form-item label="用户名" prop="user">
          <el-input v-model="form.user" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="connectDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleConnect">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { Terminal as TerminalIcon, Plus } from '@element-plus/icons-vue'
import Terminal from '@/components/ssh/Terminal.vue'

interface ConnectionForm {
  host: string
  user: string
  password: string
}

const showTerminal = ref(false)
const connectDialogVisible = ref(false)
const formRef = ref<FormInstance>()

const connection = ref<ConnectionForm>({
  host: '',
  user: '',
  password: '',
})

const form = ref<ConnectionForm>({
  host: '',
  user: '',
  password: '',
})

const rules = {
  host: [{ required: true, message: '请输入主机地址', trigger: 'blur' }],
  user: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
}

const showConnectDialog = () => {
  connectDialogVisible.value = true
}

const handleConnect = async () => {
  if (!formRef.value) return

  await formRef.value.validate((valid) => {
    if (valid) {
      connection.value = { ...form.value }
      showTerminal.value = true
      connectDialogVisible.value = false
    }
  })
}
</script>

<style scoped lang="scss">
.ssh-terminal {
  height: 100%;

  .terminal-card {
    height: 100%;
    display: flex;
    flex-direction: column;

    :deep(.el-card__body) {
      flex: 1;
      padding: 0;
      overflow: hidden;
    }

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 16px;
        font-weight: 500;

        .el-icon {
          font-size: 18px;
          color: var(--el-color-primary);
        }
      }
    }
  }
}
</style>
